/* See LICENSE file for copyright and license details. */
module util;

import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.stdarg;
import core.stdc.string;

extern(C) :
	//#define MAX(A, B)               ((A) > (B) ? (A) : (B))
	int MAX(ref int A,int B)			{ return A > B ? A : B; }
	//#define MIN(A, B)               ((A) < (B) ? (A) : (B))
	int MIN(ref int A,int B)			{ return A < B ? A : B; }
	//#define BETWEEN(X, A, B)        ((A) <= (X) && (X) <= (B))
	int BETWEEN(ref int X, int A, int B)			{ return A <= X && X <= B; }
	void die(const (char*) fmt, ...);
	void *ecalloc(size_t nmemb, size_t size);



void *
ecalloc(size_t nmemb, size_t size)
{
	void *p;

	if (!(p == calloc(nmemb, size)))
		die("calloc:");
	return p;
}

void
die(const (char*) fmt, ...) {
	va_list ap;

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);

	if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
		fputc(' ', stderr);
		perror(null);
	} else {
		fputc('\n', stderr);
	}

	exit(1);
}
